<template>
  <view>
    <web-view :src="src"></web-view>
  </view>
</template>
<script setup>
import { onShareAppMessage, onLoad } from "@dcloudio/uni-app";
import { reactive, ref } from "vue";
import { ObjectToQueryString } from "@/utils/utils";
import { onMounted } from "vue";
import { config, ShareConfig } from "./config";

const src = ref("");
const sceneObj = reactive({ scene: "", wx_scene: 0 })

const getQueryParams = (user = {}, exactParams = {}) => ({
  'accesstoken': uni.getStorageSync("TOKEN"),
  'openid': uni.getStorageSync("OPENID"),
  'unionid': uni.getStorageSync("UNIONID"),
  'memberid': user?.memberScore?.memberId || '',
  'agree': 'true',
  ...exactParams
})

onLoad(options => {
  getApp().accessLogAdd();
  sceneObj.scene = options?.scene || ""
  sceneObj.wx_scene = uni.getLaunchOptionsSync().scene
})

onMounted(async () => {
  if (!getQueryParams().openid) {
    console.log('没有数据登录')
    try {
      await getApp().getUserLoginInfo()
      const user = await getApp().getUserInfo()
      const params = getQueryParams(user, sceneObj)
      src.value = `${config.hostname}?${ObjectToQueryString(params)}`
    } catch (e) {
      uni.showToast({ title: '服务器响应错误', icon: 'none' })
    }
    return
  }
  src.value = `${config.hostname}?${ObjectToQueryString(getQueryParams({}, sceneObj))}`
})


onShareAppMessage(() => ShareConfig())
</script>

<style></style>